package _interview75;

import java.util.Arrays;

/**
 * 1679. K 和数对的最大数目
 */
public class No1679 {
    public int maxOperations(int[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length, answer = 0;
        int left = 0, right = n - 1;
        while (left < right) {
            if (nums[left] + nums[right] < k) left++;
            else if (nums[left] + nums[right] > k) right--;
            else if (nums[left] + nums[right] == k) {
                left++;
                right--;
                answer++;
            }
        }
        return answer;
    }
}
